package com.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.AlertDao;
import com.modal.AlertModel;
import com.pojo.Alert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AlertService extends ServiceImpl<AlertDao, Alert> {

    @Autowired
    private AlertDao alertDao;

    public List<Alert> getAlertByStaff(String staffId) {
        QueryWrapper q = new QueryWrapper();
        q.eq("uid", staffId);
        q.orderByDesc("time");
        List<Alert> alertList = alertDao.selectList(q);
        return alertList;
    }

    public String insertAlert(AlertModel m){
        Alert alert = new Alert();
        BeanUtil.copyProperties(m, alert);
        if (alertDao.insert(alert) < 1) {
            return "新增alert失败";
        }
        return null;
    }

    public String updateAlert(AlertModel m){
        Alert alert = alertDao.selectById(m.getId());
        if (alert == null) {
            return "alert不存在";
        }
        BeanUtil.copyProperties(m, alert);
        if (alertDao.updateById(alert) < 1) {
            return "编辑alert失败";
        }
        return null;
    }
}